﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Pr0ject0.ViewModel;

namespace Pr0ject0.Models
{
    //INCOMPLETE
    public class HiringManagerDb
    {
        private List<HiringManagerModel> managers;
        private List<JobViewModel> jobs;
        private List<JobApplicationModel> interviewReady;
        private List<JobApplicationModel> hiringReady;

        JobApplicationDB applicationDB = JobApplicationDB.GetInstance();
        JobModelDB jobDB = JobModelDB.GetInstance();
        private string connection;

        public HiringManagerDb()
        {
            connection = "DB";

            update();
        }

        public HiringManagerDb(string connectionString)
        {
            connection = connectionString;
            update();
        }

        public void update()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[connection].ToString());
            SqlCommand cmd = new SqlCommand("SELECT * FROM [Department]", conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            managers = new List<HiringManagerModel>();

            while (reader.Read())
            {
                HiringManagerModel model = new HiringManagerModel();
                model.departmentId = Convert.ToInt32(reader["DepartmentID"].ToString());
                model.userName = reader["ManagerID"].ToString();
                model.storeId = Convert.ToInt32(reader["StoreID"].ToString());
                managers.Add(model);
            }
            reader.Close();
            conn.Close();
        }

        public List<JobApplicationModel> GetInterviewList(string username)
        {
            List<JobViewModel> managerJobs = GetJobList(username);

            List<JobApplicationModel> returnlist = new List<JobApplicationModel>();

            foreach(JobViewModel j in managerJobs)
            {
                returnlist.AddRange(JobApplicationDB.GetInstance().GetStatusList(4).FindAll(x => x.jobID == j.ID));
            }

            return returnlist; //null until method completed
        }

        public List<JobApplicationModel> GetHireList(string username)
        {
            List<JobViewModel> managerJobs = GetJobList(username);

            List<JobApplicationModel> returnlist = new List<JobApplicationModel>();

            foreach (JobViewModel j in managerJobs)
            {
                returnlist.AddRange(JobApplicationDB.GetInstance().jobApplications.FindAll(x => x.jobID == j.ID && x.status == 6));
            }

            return returnlist; //null until method completed
        }

        public ManagerIndexModel GetIndexModel(string username)
        {
            ManagerIndexModel model = new ManagerIndexModel();

            model.openJobs = GetJobList(username).Count;
            model.interviewReady = GetInterviewList(username).Count;
            model.hireReady = GetHireList(username).Count;

            return model; //null until method completed
        }

        public List<JobViewModel> GetJobList(string username)
        {
            HiringManagerModel manager = managers.Find(x => x.userName == username);

            return JobModelDB.GetInstance().VJobsList.FindAll(x => x.departmentId == manager.departmentId); //null until method completed
        }

        public JobModel GetNewJobModel(string username)
        {
            JobModel job = new JobModel();
            HiringManagerModel manager = managers.Find(x => x.userName == username);

            job.departmentId = manager.departmentId;
            job.storeId = manager.storeId;
            job.jobTitles = JobTitleDB.GetInstance().jobTitles; 

            return job;
        }

        public JobApplicationModel GetApplication(string username, int id)
        {
            Predicate<JobApplicationModel> idMatch = x => x.ID == id;
            JobApplicationModel model = null;

            model = GetInterviewList(username).Find(idMatch);
            if (model == null)
            {
                model = GetHireList(username).Find(idMatch);
            }

            return model;
        }
    }
}